package com.neusoft.neuiotms.dcam.fixed.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.dto.MaintenanceChangeDTO;
import com.neusoft.neuiotms.dcam.fixed.service.MaintenanceChangeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "维保变更管理")
@RestController
@RequestMapping(value = "/api/w/maintenance/change")
public class MaintenanceChangeController {

    @Autowired
    private MaintenanceChangeService changeService;

    @ApiOperation("查询所有维保变更")
    @GetMapping
    public ResultListDTO<MaintenanceChangeDTO> index() {
        final List<MaintenanceChangeDTO> DTOs = this.changeService.findAll();
        final ResultListDTO<MaintenanceChangeDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有维保变更")
    @GetMapping("/p")
    public PageResultDTO<MaintenanceChangeDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.changeService.search(pageable, searchable));
    }

    @ApiOperation("创建变更")
    @PostMapping
    public ResultDTO<MaintenanceChangeDTO> create(@RequestBody final MaintenanceChangeDTO dto) {
        final MaintenanceChangeDTO result = this.changeService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改变更")
    @PutMapping("/{id}")
    public ResultDTO<MaintenanceChangeDTO> update(@PathVariable final Long id,
            @RequestBody final MaintenanceChangeDTO dto) {
        final MaintenanceChangeDTO result = this.changeService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("变更详情")
    @GetMapping("/{id}")
    public ResultDTO<MaintenanceChangeDTO> details(@PathVariable final Long id) {
        final MaintenanceChangeDTO dto = this.changeService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除变更")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.changeService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("根据变更修改资产的维保信息")
    @PostMapping("/changeAsset")
    public ResultDTO<MaintenanceChangeDTO> updateAsset(@RequestBody final MaintenanceChangeDTO dto) {
        final MaintenanceChangeDTO result = this.changeService.changeAssetMaintenanceInfo(dto);
        return ResultDTO.success(result);
    }

}
